Template Literal Types
code:ts
type World = "world";
type Greeting = hello ${World};
// ^ = type Greeting = "hello world"
Union型のstring literalも渡せる
code:ts
type Color = "red" | "blue";
type Quantity = "one" | "two";
type SeussFish = ${Quantity | Color} fish;
// ^ = type SeussFish = "one fish" | "two fish" | "red fish" | "blue fish"
複数渡すと直積になる
code:ts
type VerticalAlignment = "top" | "middle" | "bottom";
type HorizontalAlignment = "left" | "center" | "right";
// Takes
// | "top-left" | "top-center" | "top-right"
// | "middle-left" | "middle-center" | "middle-right"
// | "bottom-left" | "bottom-center" | "bottom-right"
declare function setAlignment(value: ${VerticalAlignment}-${HorizontalAlignment}): void;
型引数のkeyofも使える
code:ts
type PropEventSource<T> = {
on(eventName: ${string & keyof T}Changed, callback: () => void): void;
};
/// Create a "watched object" with an 'on' method
/// so that you can watch for changes to properties.
declare function makeWatchedObject<T>(obj: T): T & PropEventSource<T>;
code:ts
type PropEventSource<T> = {
on<K extends string & keyof T>
(eventName: ${K}Changed, callback: (newValue: TK) => void ): void; };
The new type aliases are Uppercase, Lowercase, Capitalize and Uncapitalize.